import numbers
class Field:
    pass

class IntField(Field):
    def __init__(self,db_column,min_value=None,max_value=None):
        self._value=None
        self.db_column=db_column
        self.min_value=min_value
        self.max_value=max_value
        if min_value is None:
            if not isinstance(min_value,numbers.Integral):
                raise ValueError('int object is needed')
            elif min_value<0:
                raise ValueError('min_value must be positive int')

        if max_value is None:
            if not isinstance(max_value,numbers.Integral):
                raise ValueError('int object is needed')
            elif max_value<0:
                raise ValueError('min_value must be positive int')
        if min_value is not None and max_value is not None:
            if min_value>max_value:
                raise ValueError('min_value must less than max_value')
    def __get__(self, instance, owner):
        return self._value
    def __set__(self, instance, value):
        if not isinstance(value,numbers.Integral):
            raise ValueError('int object is needed')
        if value<self.min_value or value>self.max_value:
            raise ValueError('value must between min_value and max_value')
        self._value=value
class CharField(Field):
    def __init__(self,db_column,max_length=None):
        self._value=None
        self.db_column=db_column
        self.max_length=max_length
        if max_length is None:
            raise ValueError('you must define max_length for charfield')
    def __get__(self, instance, owner):
        return self._value
    def __set__(self, instance, value):
        if not isinstance(value,str):
            raise ValueError('str object is needed')
        if len(value)>self.max_length:
            raise ValueError('value length excess length of max_length')
        self._value=value
class ModelMetaClass(type):
    def __new__(cls, name,bases,attrs, **kwargs):
        if name=='BaseModel':
            return super().__new__(cls,name,bases,attrs,**kwargs)
        fields={}
        for key,value in attrs.items():
            if isinstance(value,Field):
                print(key,'=',value)
                fields[key]=value
        print(fields)
        attrs_meta=attrs.get('Meta',None)
        print(attrs_meta)
        _meta={}
        db_table=name.lower()
        if attrs_meta is not None:
            table=getattr(attrs_meta,'db_table',None)
            if table is not None:
                db_table=table
        _meta['db_table']=db_table
        attrs['_meta']=_meta
        attrs['fields']=fields
        del attrs['Meta']
        return type.__new__(cls,name,bases,attrs,**kwargs)

class BaseModel(metaclass=ModelMetaClass):
    def __init__(self,**kwargs):
        print(':::::',kwargs)
        for key,value in kwargs.items():
            print(key, '==', value)
            setattr(self,key,value)

        return super().__init__()
    def save(self):
        fields=[]
        values=[]
        for key,value in self.fields.items():
            db_column=value.db_column
            if db_column is None:
                db_column=key.lower()
            fields.append(db_column)
            value=getattr(self,key)
            values.append(str(value))
        sql=f'insert into {self._meta["db_table"]} ({",".join(fields)}) values ({",".join(values)})'
        print(f'SQL:{sql}')
class User(BaseModel):
    age=IntField(db_column='age',min_value=18,max_value=30)
    sex=CharField(db_column='sex',max_length=10)
    name=CharField(db_column='name',max_length=10)
    id=IntField(db_column='user_id',max_value=1000,min_value=1)

    class Meta:
        db_table='user'


if __name__ == '__main__':

    user=User()
    user.name='AK'
    user.age=18
    user.sex='male'
    user.id=10
    user.save()

